<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  menuListService,
  menuAddService,
  menuUpdateService,
  menuDeleteService
} from '@/api/menu.js'

// 菜单数据
const menuList = ref([])
const loading = ref(false)

// 表单数据
const menuForm = ref({
  id: null,
  menuName: '',
  path: '',
  component: '',
  icon: '',
  perms: '',
  parentId: 0,
  orderNum: 0
})

// 对话框状态
const dialogVisible = ref(false)
const dialogTitle = ref('')

// 表单验证规则
const rules = {
  menuName: [{ required: true, message: '请输入菜单名称', trigger: 'blur' }],
  path: [{ required: true, message: '请输入路由路径', trigger: 'blur' }],
  component: [{ required: true, message: '请输入组件路径', trigger: 'blur' }]
}

// 获取菜单列表
const getMenuList = async () => {
  loading.value = true
  try {
    const result = await menuListService()
    menuList.value = result.data
  } catch (error) {
    ElMessage.error('获取菜单列表失败')
  } finally {
    loading.value = false
  }
}

// 添加菜单
const handleAdd = () => {
  dialogTitle.value = '添加菜单'
  menuForm.value = {
    menuId: null,
    menuName: '',
    path: '',
    component: '',
    icon: '',
    perms: '',
    parentId: 0,
    orderNum: 0
  }
  dialogVisible.value = true
}

// 修改菜单
const handleUpdate = (row) => {
  dialogTitle.value = '修改菜单'
  menuForm.value = { ...row }
  dialogVisible.value = true
}

// 提交表单
const submitForm = async () => {
  try {
    if (menuForm.value.id) {
      // 修改菜单
      await menuUpdateService(menuForm.value)
      ElMessage.success('修改成功')
    } else {
      // 添加菜单
      await menuAddService(menuForm.value)
      ElMessage.success('添加成功')
    }
    dialogVisible.value = false
    getMenuList()
  } catch (error) {
    ElMessage.error('操作失败')
  }
}

// 删除菜单
const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该菜单吗？', '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await menuDeleteService(row.id)
      ElMessage.success('删除成功')
      getMenuList()
    } catch (error) {
      ElMessage.error('删除失败')
    }
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

// 初始化加载
onMounted(() => {
  getMenuList()
})
</script>

<template>
  <div class="menu-container">
    <!-- 操作按钮 -->
    <div class="header">
      <el-button type="primary" @click="handleAdd">添加菜单</el-button>
    </div>

    <!-- 菜单列表 -->
    <el-table 
      :data="menuList" 
      v-loading="loading" 
      row-key="id"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      border
      style="width: 100%; margin-top: 20px;"
    >
      <el-table-column prop="menuName" label="菜单名称" width="180"></el-table-column>
      <el-table-column prop="path" label="路由路径" width="180"></el-table-column>
      <el-table-column prop="component" label="组件路径" width="200"></el-table-column>
      <el-table-column prop="perms" label="权限标识" width="180"></el-table-column>
      <el-table-column prop="orderNum" label="排序" width="80"></el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button type="primary" link @click="handleUpdate(scope.row)">修改</el-button>
          <el-button type="danger" link @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/修改对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="500px">
      <el-form :model="menuForm" :rules="rules" label-width="100px">
        <el-form-item label="菜单名称" prop="menuName">
          <el-input v-model="menuForm.menuName" placeholder="请输入菜单名称" />
        </el-form-item>
        <el-form-item label="路由路径" prop="path">
          <el-input v-model="menuForm.path" placeholder="请输入路由路径" />
        </el-form-item>
        <el-form-item label="组件路径" prop="component">
          <el-input v-model="menuForm.component" placeholder="请输入组件路径" />
        </el-form-item>
        <el-form-item label="权限标识" prop="perms">
          <el-input v-model="menuForm.perms" placeholder="请输入权限标识" />
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="menuForm.icon" placeholder="请输入图标名称" />
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input-number v-model="menuForm.orderNum" :min="0" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.menu-container {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: flex-start;
  margin-bottom: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>